package com.kaishengit.service;

import java.util.List;
import com.kaishengit.dao.ProjectDao;
import com.kaishengit.dao.UserProjectDao;
import com.kaishengit.entity.Project;
import com.kaishengit.entity.User;
import com.kaishengit.entity.UserProject;
import com.kaishengit.util.DateUtil;
import com.kaishengit.util.PKutil;
import com.kaishengit.util.Page;

public class ProService {
	
	private ProjectDao dao = new ProjectDao();
	private UserProjectDao userProjectDao=new UserProjectDao();
	
	public List<Project> findAll(User user){
		List<Project> list = dao.findAll(user.getId());
		return list;
	}

	public void save(String projectname, String content, String manager,
			String member) {
		

		Project project=new Project();
		String id= PKutil.getPk();
		project.setId(id);
		project.setName(projectname);
		project.setDescription(content);
		String nowTime= DateUtil.getNow();
		project.setCreatetime(nowTime);
		dao.save(project);
		

		UserProject up=new UserProject();
		up.setT_user_id(manager);
		up.setT_project_id(project.getId());
		up.setRole("manager");
		userProjectDao.save(up);
		

		String[] memberIds=member.split(",");
		for(String Id:memberIds){
			UserProject uproject=new UserProject();
			uproject.setT_user_id(Id);
			uproject.setT_project_id(project.getId());
			uproject.setRole("member");
			userProjectDao.save(uproject);
		}
		
	}

	
	public Page<UserProject> findAllByProjectId(int pageNum, String userid){
		Page<UserProject> page = new Page<UserProject>();
		page.setPageNum(pageNum);
		page.setPageSize(3);
		page.setTotalCount(dao.findAount(userid));
		page.getTotalCount();
		List<UserProject> userprojectlist = userProjectDao.findByUserId(page.getOffest(),page.getPageSize(),userid);
		page.setList(userprojectlist);
		return page;
	}

	public UserProject findById(String id, String id2) {
		return userProjectDao.findById(id, id2);
	}

	public Page<Project> findAll(int ppageNum) {
		Page<Project> page = new Page<Project>();
		page.setPageNum(ppageNum);
		page.setPageSize(3);
		page.setTotalCount(dao.findAl());
		page.getTotalCount();
		List<Project> projectlist = dao.findByAll(page.getOffest(),page.getPageSize());
		page.setList(projectlist);
		return page;
	}

	

}	

